package com.guhappy.model.contest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.guhappy.util.HibernateListener;

public class ContestPlayerHome {
	private static final Logger log = Logger.getLogger(ContestPlayerHome.class);

	private static final SessionFactory sessionFactory = getSessionFactory();

	protected static SessionFactory getSessionFactory() {
		SessionFactory sessionFactory = 
	         (SessionFactory) ServletActionContext.getServletContext()
                    .getAttribute(HibernateListener.KEY_NAME);
		return sessionFactory;
	}

	public static void persist(ContestPlayer transientInstance) {
		log.debug("persisting ContestPlayer instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public static void attachDirty(ContestPlayer instance) {
		log.debug("attaching dirty ContestPlayer instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void attachClean(ContestPlayer instance) {
		log.debug("attaching clean ContestPlayer instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void delete(ContestPlayer persistentInstance) {
		log.debug("deleting ContestPlayer instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public static ContestPlayer merge(ContestPlayer detachedInstance) {
		log.debug("merging ContestPlayer instance");
		try {
			ContestPlayer result = (ContestPlayer) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public static ContestPlayer findById(java.lang.String id) {
		log.debug("getting ContestPlayer instance with id: " + id);
		Session session=null;
		ContestPlayer contestplayer=null;
		try{
			session = sessionFactory.getCurrentSession();
			Query q = session.createQuery("from com.guhappy.model.contest.ContestPlayer c where c.id = :id");
			q.setString("id", id);
			contestplayer=(ContestPlayer) q.uniqueResult();
		}
		catch (Exception e){
			System.out.println(e);
			
		}
		return contestplayer;
	}
}
